$(function () {
    //上传
    $('.fileupload').fileupload({
        dataType: 'json',
        add: function (e, data) {
            data.submit();
            this.index = layer.load(0, {shade: false, offset: '200px'}); //0代表加载的风格，支持0-2
        },
        done: function (e, data) {
            var upload_for = $(this).attr('data-for');
            if (data.result.status == 'success') {
                $('#setting-' + upload_for).val(data.result.data.url);
                var preview_image = $('#image-preview-' + upload_for + ' img');
                if (preview_image.length > 0) {
                    preview_image.attr('src', data.result.data.url)
                } else {
                    $('#image-preview').html('<img src="' + data.result.data.url + '" style="max-height: 160px;margin-bottom:2px;padding:2px;border:1px solid #ccc;"/>');
                }
                layer.close(this.index);
                layer.msg('上传成功！', {icon: 1, offset: '200px',});
                $('#picurl').val(data.result.data.url);
            } else {
                layer.close(this.index);
                layer.msg((data.result.message != '' ? '' : '上传文件失败！') + data.result.message, {
                    icon: 2,
                    offset: '200px'
                });
            }
        },
        fail: function (e, data) {
            layer.close(this.index);
            layer.msg('上传文件失败！', {icon: 2, offset: '200px'});
        }
    });


});